package org.jeecg.modules.UserIdentity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.UserIdentity.entity.SysUserIdentity;
import org.jeecg.modules.UserIdentity.service.ISysUserIdentityService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.impl.SysUserServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 身份认证
 * @Author: jeecg-boot
 * @Date:   2023-09-04
 * @Version: V1.0
 */
@Api(tags="身份认证")
@RestController
@RequestMapping("/UserIdentity/sysUserIdentity")
@Slf4j
public class SysUserIdentityController extends JeecgController<SysUserIdentity, ISysUserIdentityService> {
	@Autowired
	private ISysUserIdentityService sysUserIdentityService;
	@Autowired
	private SysUserServiceImpl sysUserService;
	/**
	 * 分页列表查询
	 *
	 * @param sysUserIdentity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "sys_user_identity-分页列表查询")
	@ApiOperation(value="身份认证查询分页列表查询", notes="身份认证查询分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysUserIdentity>> queryPageList(SysUserIdentity sysUserIdentity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();
		sysUserIdentity.setUserId(userId);
		QueryWrapper<SysUserIdentity> queryWrapper = QueryGenerator.initQueryWrapper(sysUserIdentity, req.getParameterMap());
		Page<SysUserIdentity> page = new Page<SysUserIdentity>(pageNo, pageSize);
		IPage<SysUserIdentity> pageList = sysUserIdentityService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param sysUserIdentity
	 * @return
	 */
	@AutoLog(value = "身份认证添加")
	@ApiOperation(value="身份认证添加-添加", notes="身份认证添加-添加")
//	@RequiresPermissions("UserIdentity:sys_user_identity:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SysUserIdentity sysUserIdentity) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();
		sysUserIdentity.setUserId(userId);
		 SysUser user = sysUserService.getUserById(userId);
		 if (user.getIdentityType().equals("1")){
			 List<SysUserIdentity> data = sysUserIdentityService.getUserData(userId);
			 if (data.size()!=0){
				 return Result.ok("1");
			 }
		 }
		sysUserIdentityService.save(sysUserIdentity);
		sysUserService.updataUserAuthStatus(sysUserIdentity.getUserId(),"1");
		return Result.OK("添加成功！");
	}


	 /**
	  *   根据userID查询数据
	  *
	  * @param userId
	  * @return
	  */
	 @AutoLog(value = "身份认证接口查询")
	 @ApiOperation(value="身份认证接口查询根据UserId", notes="身份认证接口查询")
//	@RequiresPermissions("UserIdentity:sys_user_identity:add")
	 @GetMapping(value = "/getIdentityData/{userId}")
	 public Result<List<SysUserIdentity>> getIdentityData(@PathVariable String userId) {
		  List<SysUserIdentity> data = sysUserIdentityService.getUserData(userId);
		 return Result.OK(data);
	 }


	 /**
	 *  编辑
	 *
	 * @param sysUserIdentity
	 * @return
	 */
	@AutoLog(value = "身份认证修改编辑")
	@ApiOperation(value="身份认证修改编辑", notes="身份认证修改编辑")
//	@RequiresPermissions("UserIdentity:sys_user_identity:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysUserIdentity sysUserIdentity) {
		sysUserIdentityService.updateById(sysUserIdentity);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "身份认证通过id删除")
	@ApiOperation(value="身份认证通过id删除", notes="身份认证通过id删除")
//	@RequiresPermissions("UserIdentity:sys_user_identity:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		SysUserIdentity sysUserIdentity = sysUserIdentityService.getById(id);
		sysUserIdentityService.removeById(id);
		List<SysUserIdentity> data = sysUserIdentityService.getUserData(sysUserIdentity.getUserId());
		if (data.size()==0){
			sysUserService.updataUserAuthStatus(sysUserIdentity.getUserId(),"0");
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "身份认证批量删除")
	@ApiOperation(value="身份认证批量删除", notes="身份认证批量删除")
//	@RequiresPermissions("UserIdentity:sys_user_identity:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysUserIdentityService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "sys_user_identity-通过id查询")
	@ApiOperation(value="身份认证通过id查询", notes="身份认证通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysUserIdentity> queryById(@RequestParam(name="id",required=true) String id) {
		SysUserIdentity sysUserIdentity = sysUserIdentityService.getById(id);
		if(sysUserIdentity==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysUserIdentity);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysUserIdentity
    */
//    @RequiresPermissions("UserIdentity:sys_user_identity:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysUserIdentity sysUserIdentity) {
        return super.exportXls(request, sysUserIdentity, SysUserIdentity.class, "sys_user_identity");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("UserIdentity:sys_user_identity:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysUserIdentity.class);
    }

}
